//
//  DDChannel.m
//  NetEaseNews
//
//  Created by dqw on 15/7/24.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "DDChannel.h"

@implementation DDChannel


- (instancetype)initWithDic:(NSDictionary *)dic
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
+ (instancetype)channelWithDic:(NSDictionary *)dic
{
    return [[self alloc]initWithDic:dic];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}
- (NSString *)urlStr
{
//    T1348647853363
//    return [NSString stringWithFormat:@"article/headline/%@/0-140.html", self.tid];
    return @"article/headline/T1348647853363/0-140.html";
}

+ (NSArray *)channels
{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"topic_news.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
    NSArray *array = dic[@"tList"];
    
    //字典转模型
    NSMutableArray *mArray = [NSMutableArray array];
    for (NSDictionary *dataDic in array) {
        DDChannel *channel = [self channelWithDic:dataDic];
        [mArray addObject:channel];
    }
    return [mArray sortedArrayUsingComparator:^NSComparisonResult(DDChannel *obj1, DDChannel *obj2) {
        return [obj1.tid compare:obj2.tid];
    }];
//    NSString *path = [[NSBundle mainBundle]pathForResource:@"topic_news.json" ofType:nil];
//    NSData *data = [NSData dataWithContentsOfFile:path];
//   NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
//    NSString *firstKey = dict.keyEnumerator.nextObject;
//    NSArray *arr = dict[firstKey];
//    NSMutableArray *arrM = [NSMutableArray array];
//    for (NSDictionary *dic in arr) {
//        DDChannel *channel = [DDChannel channelWithDic:dic];
//        [arrM addObject:channel];
//    }
//    return [arrM.copy sortedArrayUsingComparator:^NSComparisonResult(DDChannel * obj1, DDChannel * obj2) {
//        return [obj1.tname compare:obj2.tname];
//    }];
    
}
@end
